/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ocr.msg;

import cn.ac.iie.di.datadock.rdata.exchange.client.v1.session.RESendSession;
import cn.ac.iie.di.ocr.msg.JsonMsgSender.SessionPool;
//import cn.ac.iie.di.tools.LongHashCode;
//import cn.ac.iie.di.tools.MMUrlPath;
//import cn.ac.iie.di.tools.TimeTransfer;
import java.io.File;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Set;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author lenovo-pc
 */
public class JsonMsgTask implements Runnable {

    private final List<List> jsonList;

    private HashMap<String, String> JsonData = new HashMap<>();//存储Json中的数据1
    static private final Logger logger = Logger.getLogger(JsonMsgTask.class.getName());
    static private final AtomicInteger count = new AtomicInteger(0);

//    TrsCkmSoapClient client = new TrsCkmSoapClient();
    public static void printLog(int n) {
        if (count.addAndGet(n) > 500) {
            boolean print = false;
            int cur = 0;
            synchronized (count) {
                cur = count.get();
                if (cur > 500) {
                    count.addAndGet(-cur);
                    print = true;
                }
            }
            if (print) {
                logger.info("The imsg parser " + cur);
            }
        }
    }

    private final SessionPool pool;

    public JsonMsgTask(List<List> jsonList, SessionPool pool) {
        this.jsonList = jsonList;
        this.pool = pool;

    }

    @Override
    public void run() {
//        client.SetServer(GlobalParas.IP_TRS, "admin", "trsadmin");

        RESendSession session = null;
        String str_pid = "";
        long line = 0;
        try {
            session = pool.borrowSession();
            for (List request : jsonList) {
                if (request != null && request.size() != 0) {
                    for (Object obj : request) {
                        Map update = (Map) obj;
                        try {
                            Set<String> key = update.keySet();
                            for (String s : key) {
                                session.setString(s, update.get(s).toString());
                            }
                            try {
                                session.add();
                                line++;
                            } catch (Exception e) {
                                sendMSG(session);
                                logger.info("MSG successful send " + line);
                                line = 0;
                                session.add();
                                line++;
                            }

                        } catch (Exception ex) {
                            logger.error("The session add is wrong:" + ex, ex);
                        }
                    }

                }
            }
            sendMSG(session);
            logger.info("MSG successful send " + line);
            line = 0;
        } catch (Exception ex) {
            logger.error("know group Task error " + ex, ex);
        } finally {
            if (session != null) {
                pool.returnSession(session);
            }
        }
    }

    public void sendMSG(RESendSession session) {
        while (true) {
            try {
                session.flush();
                break;
            } catch (Exception ex) {
                logger.info("what's up buddy ? how's it going ?" + ex, ex);
            }
        }
    }

    public static String getException(Exception e) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PrintStream pout = new PrintStream(out);
        e.printStackTrace(pout);
        String ret = new String(out.toByteArray());
        pout.close();
        try {
            out.close();
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(JsonMsgSender.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ret;
    }
}
